local game = require "club.threecard.blackjack"
local ingame = 
{
    throttleable = true
}

local phase_name = {
    [0] = "空闲",
    [1] = "准备",
    [2] = "发牌",
    [3] = "保险",
    [4] = "保险展示",
    [5] = "玩家",
    [6] = "庄家",
    [7] = "结算"
}

local actions_name = 
{
    [1] = "要牌",
    [2] = "分牌",
    [3] = "停牌",
    [4] = "翻倍",
}

ingame.state = 
{
    PHASE = 
    {
        NULL = -1,
        IDLE = 0,
        PREPARE = 1,
        DEAL = 2,
        INSURE = 3,
        INSURE_RESULT = 4,
        PLAYER = 5,
        DEALER = 6,
        SETTLE = 7,
    },

    selected_bet_index =1,
    bet_amounts = {},

    phase = -1;
    phase_name = "";
    countdown = 0;

    current_player = nil;
    action_ids = {};
    current_active_hand_index = 0;
    player_countdown = 0;
    insured = nil;
    self_playing = false;
    banker_cards = {};
    scene = nil;
}

ingame.getters = 
{
    bet_value = function(state)
        local value = state.bet_amounts[state.selected_bet_index];
        if value == nil then
            return 0;
        end

        return value;
    end,

    tip_phase = function(state)
        if state.phase == state.PHASE.PREPARE then
            return ""
        elseif state.phase == state.PHASE.DEAL then
            return ""
        elseif state.phase == state.PHASE.INSURE then
            if state.insured == nil then
                return string.format("%s：%s", game.i18n.text("blackjack:保险提示"), state.countdown);
            else
                if state.insured == true then
                    return string.format("%s: %s", game.i18n.text("blackjack:保险提示_买了"), state.countdown);    
                else
                    return string.format("%s: %s", game.i18n.text("blackjack:保险提示_没买"), state.countdown);
                end
            end
        else
            return ""
        end

        return "未定义"
    end,

    tip_insure_result = function(state)
        if state.blackjack == 0 then
            return game.i18n.text("blackjack:无21点");
        elseif state.blackjack == 1 then
            return game.i18n.text("blackjack:有21点");
        end 

        return "未定义";
    end,
}

local timer = nil;

ingame.mutations = 
{
    on_bet_amounts = function(store, state, bet_amounts)
        state.bet_amounts = bet_amounts;
        store.emit("更新下注档位");
    end,

    on_phase = function(store, state, args)
        state.insured = nil

        state.phase_name = phase_name[args.phase_id] or "未知";
        state.countdown = math.ceil(args.countdown/1000);
        state.phase = args.phase_id;

        if state.phase == state.PHASE.IDLE then
            state.self_playing = false;
        end

        if timer then
            game.coroutine.stop(timer);
        end

        if state.countdown > 0 then
            timer = game.coroutine.start(function()
                while state.countdown > 0 do
                    game.coroutine.wait(1);
                    state.countdown = state.countdown - 1;
                    store.emit("countdown变更");
                end
            end)
        end

        store.emit("阶段变更");
    end,

    on_insure_result = function(store, state, args)
        state.blackjack = args.blackjack;
        state.claim_info = args.claim_info;
        store.emit("保险结果");
    end,

    on_player_trun = function(store, state, args)
        state.current_player = args.player;
        state.current_active_hand_index = args.active_hand_index;
        state.player_countdown = args.countdown/1000;

        state.current_player_splitted = args.splitted==1

        store.emit("玩家回合", state.player_countdown);
    end,

    on_player_split = function(store, state, args)
        store.emit("玩家分牌", args);
    end,

    on_player_insure = function(store, state, args)
        store.emit("玩家购买保险", args)
    end,

    on_banker_deal = function(store, state, card_ids)
        state.banker_cards = card_ids;

        store.emit("庄家摸牌");
    end,

    on_banker_reveal = function(store, state, card_id)
        store.emit("庄家展示底牌", card_id);
    end,

    on_game_result = function(store, state, result_info)
        store.emit("结算", result_info);
    end,

    on_player_deal = function(store, state, args)
        local player = args.player;
        local card_ids = args.card_ids;
        local hand_index = args.hand_index;

        store.emit("玩家摸牌", {no = player.seat.no, card_ids = card_ids, hand_index = hand_index});
    end,

    on_player_bet = function(store, state, args)
        local player = args.player;
        local amount = args.amount;
        store.emit("玩家下注", {no = player.seat.no, amount = amount});
    end,

    on_player_double = function(store, state, player)
        store.emit("玩家翻倍", player.seat.no)
    end,

    on_sync_actions = function(store, state, action_ids)
        state.action_ids = action_ids;
        store.emit("同步操作");
    end,

    on_game_start = function(store, state, scene)
        local players = scene.players;
        local self_player = game.kbe.player();
        state.self_playing = false;
        for _, player in ipairs(players) do
            if player.no == self_player.seat.no then
                state.self_playing = true;
                break;
            end
        end

        store.emit("游戏开始", scene);
    end,

    on_game_init = function(store, state, scene)
        state.scene = scene;
        local self_player = game.kbe.player();
        state.self_playing = false;
        local players_data = {}
        for _, player in ipairs(scene.players) do
            if player.no == self_player.seat.no then
                state.self_playing = true;
            end
            players_data[player.no] = player
        end

        local current_player = players_data[scene.current_no]

        if not current_player then
            state.current_player = nil
        else
            state.current_player = game.store.room.state.playing_players[scene.current_no]
            state.player_countdown = scene.left_time/1000;
            state.current_active_hand_index = current_player.active_hand_index
            state.current_player_splitted = #current_player.hand2_cards > 0
        end

        state.action_ids = scene.action_ids

        store.emit("恢复游戏场景")
    end,

    --[[===============ui触发=================]]--
    on_bet_change = function(store, state, args)
        local value = state.selected_bet_index + args.changed;
        if value > #state.bet_amounts then
            value = 1;
        end
        if value < 1 then
            value = #state.bet_amounts;
        end

        state.selected_bet_index = value;

        store.emit("改变下注");

        if value == #state.bet_amounts then
            store.emit("最大注")
        end
    end,

    on_bet_max = function(store, state, args)
        state.selected_bet_index = #state.bet_amounts;
        store.emit("最大注");
    end,

    insure = function(store, state, b)
        state.insured = b;
        store.emit("确认保险")
    end,
}

return ingame